var vm = new Vue({
    el: "section",
    data: {
        userid: "",
        friendList: [],
        resultList: [],
    },
    methods: {
        // 选择好友
        selectFriend(userid, userImg){
            let isInResult = false
            this.resultList.forEach((item, index)=>{
                if(item.userid === userid){
                    isInResult = true
                    return
                }
            })
            if(isInResult){//移除该用户
                this.resultList = this.resultList.filter((item, index)=> item.userid !== userid)
                document.querySelector(`li[uid='${userid}'] .top .checkBox`).classList.remove('active')
            }else{//添加用户
                this.resultList.push({
                    userid: userid,
                    userImg: userImg
                })
                document.querySelector(`li[uid='${userid}'] .top .checkBox`).classList.add('active')
            }
            
        },
        //取消选择
        close(){
            this.resultList = []
            let lis = document.querySelectorAll(".ul1 li")
            for(let i=0;i<lis.length;i++){
                lis[i].querySelector(".top .checkBox").classList.remove('active')
            }
        },
        // 完成选择
        submit(){
            console.log("提交")
            let toast1 = this.$toast.loading({
                message: '加载中...',
                forbidClick: true,
                loadingType: 'circular',
                duration: 10000
              });
            let userList = []
            this.resultList.forEach((item, index)=>{
                userList.push(item.userid)
            })
            if(userList.length > 0){
                console.log(userList)
                // 发送请求
                let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
                let method = `createGroup_ERG8671j174638JK17T`
                let data = {
                    userid: this.userid,
                    userList: userList
                }
                axios.post(`${url}/${method}`, data)
                .then(res=>{
                    toast1.clear()
                    if(res.data.code == 0){
                        this.$toast.success({
                            message: "成功！",
                            forbidClick: true,
                            duration: 1500,
                        })
                        setTimeout(()=>{
                            window.history.go(-1)
                        }, 300)
                    }else{
                        this.$toast({
                            message: `服务出错[${res.data.code}]`,
                            forbidClick: true
                        })
                    }
                })
            }else{
                this.$toast({
                    message: '请选择帮友',
                    forbidClick: true
                })
            }
        },
    },
    mounted(){
        let toast2 = this.$toast.loading({
            message: '加载中...',
            forbidClick: true,
            loadingType: 'circular',
            duration: 10000
        });
        let userInfo = localStorage.getItem('XGB-user')
        this.userid = JSON.parse(userInfo).id
        // 获取好友列表
        let url = `https://fc-mp-e8e2bde5-0c0c-4622-87b5-66f8b08c3856.next.bspapp.com/getArticleList1.0`
        let method = `getFriend_A681746F4d1v4d6VNT7816`
        let data = {
            userid: this.userid
        }
        axios.post(`${url}/${method}`, data)
        .then(res=>{
            toast2.clear()
            if(res.data.code == 0){
                this.friendList = res.data.data.data_friendList.sort((a, b) => a.note.localeCompare(b.note));
            }else{
                this.$toast({
                    message: `服务出错[${res.data.code}]`,
                    forbidClick: true
                })
            }
        })
        
    }
})